class Player extends imageClass{
    constructor(game){
        super(game, "r1")
        this.game = game
        this.setup()
    }

    setup(){
        this.frameCount = config.action_speed
        this.frameIndex = 0
        this.frame = {}
        this.x = 150
        this.y = 250
        this.dy = -config.jump_hight
        this.inversion = false
        this.frame["run"] = []
        this.frame["jump"] = []
        this.frame["stop"] = []

        for (var i = 1; i <= config.run_pic; i ++){
            var name = `r${i}`
            var t = this.game.textureByName(name)
            this.frame["run"].push(t) 
        }
        for (var i = 1; i <= config.jump_pic; i ++){
            var name = `j${i}`
            var t = this.game.textureByName(name)
            this.frame["jump"].push(t) 
        }
        for (var i = 1; i <= config.stop_pic; i ++){
            var name = `s${i}`
            var t = this.game.textureByName(name)
            this.frame["stop"].push(t) 
        }
    }

    draw(){
        self = this
        var ctx = self.game.ctx
        var x = self.x + self.w/2
        var y = self.y + self.h/2
        var degree = (self.dy < 0) ? -45 : 45

        ctx.save()
        if (self.jumping()){
            if (!self.inversion){
                ctx.translate(x, y)
            }
            else {
                ctx.scale(-1,1)
                ctx.translate(-x, y)
            }
            ctx.rotate(degree * Math.PI/180)
            ctx.drawImage(self.texture, -self.w/2, -self.h/2)
        } else {
            if (self.inversion){
                ctx.scale(-1,1)
                x = -self.x - self.w
            }
            ctx.drawImage(self.texture, x, y)
        }
       self.game.ctx.restore()
    }

    moveLeft ()  {
        if (!this.jumping()){
            this.act("run", "left")
        }
        if (this.x >= 0){
            this.x -= config.player_speed
        }
    }

    moveRight (){
        if (!this.jumping()){
            this.act("run", "right") 
        }
        if (this.x <= 500 - this.w){
            this.x += config.player_speed
        }
    }

    jumping(){
        return (this.action == "jump")
    }

    setJump(){
        self.act("jump")
    }

    doJump(){
        self = this
        if (self.y <= 250){
            self.y += self.dy
            self.dy += config.gravity
        }
        else{
            self.y = 250
            self.dy = -config.jump_hight
            self.act("stop")
        }
    }

    act(name, status){
        this.action = name
        if (typeof(status) == "undefined"){
            return
        }
        this.inversion = (status == "left")
    }

    isActionChange(){
        return (this.action != this.last_action)
    }

    frames(){
        return this.frame[this.action]
    }

    animate(){
        var self = this

        if (this.jumping()){
            this.doJump()
        }

        if (self.game.idle && !self.jumping()){
            self.act("stop")
        }
        
        self.frameCount -- 
        if (self.frameCount == 0){
            self.frameCount = config.action_speed
            self.frameIndex = (self.frameIndex + 1) % self.frames().length
            self.texture = self.frames()[self.frameIndex]
        }
    }

    update(){
        this.animate()
        this.last_action = this.action
    }

}